require("../util.js");
require("../component/components.js");
require("../component/purchaseHeader.js");
require("../component/basicSlide.js");
require("../component/footer.js");
require("../../jsMt/util.js");
var userInfo=JSON.parse(sessionStorage.getItem("userInfo"));
// if(!userInfo){
//     location.href="/loginIndex"
// }
var vm = new Vue({
    el:"#purchaseIndex",
    data:{
        isNav:1,
        mainList:{},//主页接口
        luckyVoucher:[],
        hours:"",//时
        minutes:"",//分
        seconds:"",//秒
        endDate:"",
        endSecond:"",
        startsecond:"",
        shopsList:{},
        categoryId:"",
        cartCountNum:"",
        productList:{},
        productList1:[],
        productList2:[],
        pageNumber:1
    },
    mounted:function(){
        this.getMainList();
        this.cartCount();
        this.bottomProductList();
    },
    methods:{
        bgimg:function(url){
            url = url?url:"";
            return {
                "background-image":'url("'+url+'"),url("")'
            }
        },
        //乐购主页接口
        getMainList:function(){
            var that=this;
            this.ajax({url:"/purchase/getMainList",dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.mainList=b.json.data;
                    that.luckyVoucher=that.mainList.luckyVoucher[0];
                    that.shopsList=that.mainList.hotsShops[0];
                    that.categoryId=that.shopsList&&that.shopsList.categoryId;
                    if(that.luckyVoucher&&that.luckyVoucher.sortDate==0){
                        that.endDate=that.luckyVoucher.endDate;
                        that.endSecond=parseInt(that.endDate)*60*60;
                        that.compareDate(that.luckyVoucher.currentTime);
                    }
                    // console.log(that.endDate)
                    // console.log(that.endSecond)
                }else{
                    vm.$refs.prompt.toast("乐购主页接口！")
                }
            });
        },
        getShop:function(categoryId){
            var that=this;
            that.mainList.hotsShops.forEach(function(val){
                if(val.categoryId==categoryId){
                    that.categoryId=val.categoryId;
                    that.shopsList=val;
                }
            });
        },
        //购物车数量
        cartCount:function(){
            var that=this;
            if(userInfo&&userInfo.loginUid){
                this.ajax({url:"/purchase/cartCount",data:{loginUid:userInfo.loginUid},dataType:"json",method:"post"},function(b){
                    if(b.json.code==0){
                        that.cartCountNum=b.json.data;
                    }else{
                        // if(b.json.msg="用户未登录"){
                        //    //this.toLogin();
                        // }else{
                        //     vm.$refs.prompt.toast(b.json.msg||"获取购物车数量失败！");
                        // }
                        vm.$refs.prompt.toast(b.json.msg||"获取购物车数量失败！");
                    }
                });
            }else{
                that.cartCountNum=0;
            }

        },
        //乐购折扣列表
        bottomProductList:function(){
            var that=this,data;
            data={
                loginUid:userInfo&&userInfo.loginUid,
                pageNumber:that.pageNumber
            };
            this.ajax({url:"/purchase/bottomProductList",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.productList=b.json.data;
                    that.productList1=that.productList.list;
                    if(that.productList.pageNumber>1){
                        if(that.productList.list.length>0) {
                            that.productList2 = that.productList2.concat(b.json.data.list);
                        }
                    }else{
                        that.productList2= that.productList1;
                    }
                }else{
                    that.productList={};
                    that.productList1=[];
                    that.productList2=[];
                    vm.$refs.prompt.toast("获取折扣列表失败！")
                }
            });
        },
        compareDate:function(item){
            var that=this;
            console.log(item)
            var dataList=item.split(":");
            var hour,minute,second;
            hour=parseInt(that.endDate)-parseInt(dataList[0]);
            if(dataList[1]>0||dataList[2]>0){
                hour--;
                minute=60-parseInt(dataList[1]);
                if(dataList[2]>0){
                    minute--;
                    second=60-parseInt(dataList[2]);
                }
            }
            console.log(hour+"时"+minute+"分"+second+"秒");
            that.hours=hour;
            that.minutes=minute;
            that.seconds=second;
            console.log(that.hours)
            if(that.hours>=0&&that.minutes>=0&&that.seconds>0){
                a=setInterval(this.leftTimer,1000);
            }

            //this.leftTimer();
        },
        //时分秒倒计时
        leftTimer:function(){
            var that=this;
            that.hours=Number(that.hours);
            that.minutes=Number(that.minutes);
            that.seconds=Number(that.seconds);
            if(that.hours==0&&that.minutes==0&&that.seconds==0){
                clearInterval(a);
            }else{
                that.seconds--;
                if(that.seconds == 0){
                    that.minutes--;
                    that.seconds = 59;
                }
                if(that.minutes < 0 && that.hours > 0){
                    that.hours--;
                    that.minutes = 59;
                }
                if (that.hours <= 9) that.hours = '0' + that.hours;
                if (that.minutes <= 9) that.minutes = '0' + that.minutes;
                if (that.seconds <= 9) that.seconds = '0' + that.seconds;
            }
            console.log(that.hours)
        },
        //跳转页面
        getNext:function(type,productId){
            if(type==1){
                location.href="/purchase/goodsDetails?productId="+productId+"&type=1";
            }else if(type==2){
                location.href="/purchase/shoppingCart";
            }else if(type==3){
                if(productId.categoryId==-1){
                    location.href="/purchase/goodsClassify";
                }else{
                    location.href="/purchase/goodsList?categoryId="+productId.categoryId+"&name="+encodeURI(productId.categoryName);
                }
            }else if(type==4){
                location.href="/purchase/newlyweds";
            }else if(type==5){
                location.href="/purchase/burstingActivity?type=1"
            }else if(type==6){
                location.href="/happyi/collage";
            }else if(type==7){
                location.href="/cityAd/citySearch?type=2";
            }else if(type==8){
                location.href="/enjoy";
            }else if(type==9){
                location.href="/purchase/goodsList"
            }
        }
    }
});
var a;
function checkTime(i){ //将0-9的数字前面加上0，例1变为01
    if(i<10){
        i = "0" + i;
    }
    return i;
}
//文档高度
function getDocumentTop() {
    var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
    if (document.body) {
        bodyScrollTop = document.body.scrollTop;
    }
    if (document.documentElement) {
        documentScrollTop = document.documentElement.scrollTop;
    }
    scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;
    return scrollTop;
}
//可视窗口高度
function getWindowHeight() {
    var windowHeight = 0;
    if (document.compatMode == "CSS1Compat") {
        windowHeight = document.documentElement.clientHeight;
    } else {
        windowHeight = document.body.clientHeight;
    }
    return windowHeight;
}
//滚动条滚动高度
function getScrollHeight() {
    var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0;
    if (document.body) {
        bodyScrollHeight = document.body.scrollHeight;
    }
    if (document.documentElement) {
        documentScrollHeight = document.documentElement.scrollHeight;
    }
    scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
    return scrollHeight;

}
window.onscroll=function(){
    if(getDocumentTop()>120){
        vm.isNav=2;
    }else{
        vm.isNav=1;
    }
    loadFlag = vm.productList.lastPage;
    //监听事件内容
    if(getScrollHeight()==getWindowHeight() + getDocumentTop()){
        //当滚动条到底时,这里是触发内容
        if(!loadFlag){
            vm.loadFlag=true;
            vm.pageNumber++;
            vm.bottomProductList();
            lazyload();
        }else{
            vm.$refs.prompt.toast("没有更多了！")
        }
    }
};
function lazyload(){
    $("img.lazy").lazyload();
}
